package com.pxsw.mobile.hcd.act;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mdx.mobile.activity.MActivity;
import com.pxsw.mobile.hcd.R;
import com.pxsw.mobile.hcd.widget.MToast;

public class StreamPhotoSubmitAct extends MActivity {

	private EditText edit;
	private Button back, btn;
	private TextView title;
	private String picpathcrop;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			closeComment();
		}
		return super.onKeyDown(keyCode, event);
	}

	public void closeComment() {
		if (edit.getText().toString().trim().length() > 0) {
			new AlertDialog.Builder(StreamPhotoSubmitAct.this)
					.setIcon(R.drawable.dialog_icon)
					.setTitle("系统提示")
					.setMessage("图片尚未发布")
					.setPositiveButton("放弃",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									StreamPhotoSubmitAct.this.finish();
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

								}
							}).create().show();
		} else {
			StreamPhotoSubmitAct.this.finish();
		}
	}

	public void sendComment() {

	}

	@Override
	protected void create(Bundle arg0) {
		setContentView(R.layout.streamcomment);
		edit = (EditText) findViewById(R.stream_comment.comment);
		back = (Button) findViewById(R.header.back);
		btn = (Button) findViewById(R.header.button);
		title = (TextView) findViewById(R.header.title);
		back.setOnClickListener(new OnClick());
		btn.setText(getResources().getString(R.string.commentsubmit));
		btn.setOnClickListener(new OnClick());
		title.setText(R.string.transmitttitle);
		edit.setHint(R.string.transmithits);
		btn.setText(R.string.transmitsubmit);
		picpathcrop = Environment.getExternalStorageDirectory().getPath()
				+ "/images/tempcrop.jpg";
	}

	public class OnClick implements OnClickListener {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.header.back:
				closeComment();
				break;
			case R.header.button:
				if (edit.getText().toString().trim().length() < 1) {
					new MToast(v.getContext(), "说点什么吧", Toast.LENGTH_SHORT)
							.show();
					break;
				}
				dataLoad(null);
				break;
			}
		}
	}

//	@Override
//	public void disposeMessage(Son son) throws Exception {
//		if (son.mgetmethod.equals("MUploadImg")) {
//			Retn.Msg_Retn.Builder build = (Retn.Msg_Retn.Builder) son.build;
//			if (build.getErrorcode() == 1) {
//				Toast.makeText(StreamPhotoSubmitAct.this, "发布成功",
//						Toast.LENGTH_LONG).show();
//				if (Frame.HANDLES.get("StreamMeAct").size() > 0) {
//					Frame.HANDLES.get("StreamMeAct").get(0).sent("");
//					
//				}
//				if (Frame.HANDLES.get("StreamIndexAct").size() > 0) {
//					Frame.HANDLES.get("StreamIndexAct").get(0).sent("");
//				}
//				if (Frame.HANDLES.get("StreamPhotoAct").size() > 0) {
//					Frame.HANDLES.get("StreamPhotoAct").get(0).sent("");
//				}
//				finish();
//			} else {
//				Toast.makeText(StreamPhotoSubmitAct.this, "发布失败",
//						Toast.LENGTH_LONG).show();
//			}
//		}
//	}

	private byte[] getImgData() {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] data = null;
		try {

			InputStream is = new FileInputStream(picpathcrop);
			byte[] buff = new byte[1024];
			if (is != null) {
				while (is.read(buff, 0, buff.length) != -1) {
					baos.write(buff, 0, buff.length);
				}
				is.close();
				baos.flush();
				data = baos.toByteArray();
			}
		} catch (IOException e1) {
			Log.e("log", "get img", e1);
		}
		return data;
	}

//	@Override
//	public void dataLoad(int[] types) {
//		ByteString.Output imgFile = ByteString.newOutput();
//		try {
//			imgFile.write(getImgData());
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//
//		ImgRequest.Msg_ImgRequest.Builder imgRequest = ImgRequest.Msg_ImgRequest
//				.newBuilder();
//		Msg_PostImg.Builder postImg = Msg_PostImg.newBuilder();
//		postImg.setDescription("");
//		postImg.setImgName(edit.getText().toString());
//		postImg.setValueByte(imgFile.toByteString());
//
//		imgRequest.setApkId(F.APKID);
//		imgRequest.setContentTypeId("-1");
//		imgRequest.setCreatorId(F.businessid + ";" + F.USER_ID); // 商家ID +
//																	// 登录用户账号
//		imgRequest.setModuleId(F.modelids[0]);
//		imgRequest.addPostImg(postImg);
//
//		loadData(new Updateone[] { new Updateone("MUploadImg", imgRequest, 0,
//				Retn.Msg_Retn.newBuilder()), });
//	}
}
